using Engine.FileFormats3D.WR2TerrainLoader;
using SharpDX;
using System;
using System.Windows.Forms;

namespace Engine.DebugPanels
{
    internal partial class WR2ScenarioDebugPanel : Form
    {
        #region Private Fields

        private QADFile qadFile;
        private VTXFile vtxFile;

        #endregion Private Fields

        #region Internal Constructors

        internal WR2ScenarioDebugPanel()
        {
            InitializeComponent();
            this.Show();

            while (this.Visible)
            {
                Application.DoEvents();
            }

            //WR2TerrainLoader.WR2Surface surface = new WR2Surface();
            //surface.mappings1..M11 = 1.0f;
            //surface.mappings1.matrix.M12 = 1.0f;
            //surface.mappings1.matrix.M23 = 1.0f;
            //float winkel = surface.mappings1.ScaleX;
            //surface.mappings1.ScaleX = 20;
            //float res =  surface.mappings1.RotationX;
            //float tes = surface.mappings1.ScaleX;
        }

        #endregion Internal Constructors

        #region Private Methods

        private void button_LoadScenario_Click(object sender, EventArgs e)
        {
            //string filename = @"E:\Games\World Racing 2\Scenarios\Italien\V1\Italien";
            //string filename = @"E:\Games\World Racing 2\Scenarios\LWOTest\V1\LWOTest";
            //string filename = @"E:\Games\World Racing 2\Scenarios\546454\V1\546454";
            string filename = @"C:\Games\World Racing 2\Scenarios\546454\V1\546454";
            vtxFile = VTXFile.FromFile(filename + ".vtx");
            //vtxFile.Save(filename + ".vtx");
            //vtxFile.Save(filename + "NEU.vtx");
            qadFile = QADFile.FromFile(filename + ".qad");
            //qadFile.Save(filename + "Neu.qad");
        }

        private void numericUpDown_SurfaceChunk_ValueChanged(object sender, EventArgs e)
        {
            this.numericUpDown_SurfaceChunk.Maximum = this.qadFile.surfaceChunks.Count - 1;
            this.richTextBox_surfaceBlock.Clear();
            this.richTextBox_surfaceBlock.Text = this.qadFile.surfaceChunks[(int)this.numericUpDown_SurfaceChunk.Value].ToString();
        }

        private void numericUpDown_Vertex_ValueChanged(object sender, EventArgs e)
        {
            this.richTextBox1.Clear();
            int block = (int)this.numericUpDown_VertexBlock.Value - 1;
            if (this.vtxFile.vertexBlocks[block] != null)
            {
                richTextBox1.AppendText(this.vtxFile.vertexBlocks[block].engineVertices[(int)this.numericUpDown_Vertex.Value].ToString());
                Color4 color = this.vtxFile.vertexBlocks[block].engineVertices[(int)this.numericUpDown_Vertex.Value].vertexColor;

                this.panel1.BackColor = System.Drawing.Color.FromArgb(color.ToRgba());
            }
        }

        private void numericUpDown_VertexBlock_ValueChanged(object sender, EventArgs e)
        {
            this.numericUpDown_VertexBlock.Minimum = 1;// this.vtxFile.vertexBlocks.Length;
            this.numericUpDown_VertexBlock.Maximum = this.vtxFile.vertexBlocks.Length;

            this.numericUpDown_Vertex.Minimum = 1;
            this.numericUpDown_Vertex.Minimum = 0;
            int block = (int)this.numericUpDown_VertexBlock.Value - 1;

            if (this.vtxFile.vertexBlocks[block] != null)
            {
                this.numericUpDown_Vertex.Maximum = this.vtxFile.vertexBlocks[block].engineVertices.Count;
            }
            else
            {
                this.numericUpDown_Vertex.Maximum = 0;
            }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            richTextBox_ObjectInstance.Clear();
            richTextBox_ObjectInstance.Text = this.qadFile.objectProperties[(int)this.numericUpDown1.Value].ToString();
        }

        #endregion Private Methods
    }
}